package com.example.innovate.controller;

/*
    2021.9.13 update/
 */

import com.example.innovate.bean.Result;
import com.example.innovate.bean.SuperAdminVO;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.SuperAdminInfoDO;
import com.example.innovate.domain.UserInfoDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.SuperAdminService;
import com.example.innovate.service.TokenService;
import com.example.innovate.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/api/login")
@Api(tags = "登录接口")
public class LoginController {

    @Autowired
    SuperAdminService superAdminService;
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    TokenService tokenService;

    @ResponseBody
    @PostMapping(value = "/superAdminLogin")
    @ApiOperation("超级管理员登录接口")
    public Result superAdminLogin(@RequestParam("username") String username,
                                  @RequestParam("password") String password,
                                  HttpServletResponse response) throws ParseException{
        SuperAdminInfoDO superAdminInfoDO1 = superAdminService.getAdminByName(username);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        Date d1 = sdf.parse(now);

        //用户名存在
        if (superAdminInfoDO1 != null) {
            if(password.equals( superAdminInfoDO1.getAdminPassword() )){
                if(superAdminInfoDO1.getAdminIsFrozen().equals("true")){
                    String frozen = superAdminInfoDO1.getAdminTime();
                    Date d2 = sdf.parse(frozen);
                    if((((d1.getTime() - d2.getTime()) / 1000) / 60)>=10){
                        superAdminInfoDO1.setAdminTimes(0);
                        superAdminInfoDO1.setAdminIsFrozen(false);
                        superAdminInfoDO1.setAdminTime(null);
                        superAdminService.update(superAdminInfoDO1);
                        String token = tokenService.getSuperAdminToken(superAdminInfoDO1);
                        SuperAdminVO superAdmin = new SuperAdminVO();
                        superAdmin.setAdminId(superAdminInfoDO1.getAdminId());
                        superAdmin.setAdminIsAdmin(true);
                        superAdmin.setAdminIsSuperAdmin(true);
                        superAdmin.setAdminToken(token);
                        return new Result(200,"success","登陆成功", superAdmin);
                    }
                    else{
                        long s = 10-(((d1.getTime() - d2.getTime()) / 1000) / 60);
                        return new Result(400,"error","账号冻结中，预计还有"+s+"分钟", null);
                    }
                }
                else{
                    superAdminInfoDO1.setAdminTimes(0);
                    superAdminService.update(superAdminInfoDO1);
                    String token = tokenService.getSuperAdminToken(superAdminInfoDO1);
                    SuperAdminVO superAdmin = new SuperAdminVO();
                    superAdmin.setAdminId(superAdminInfoDO1.getAdminId());
                    superAdmin.setAdminIsAdmin(true);
                    superAdmin.setAdminIsSuperAdmin(true);
                    superAdmin.setAdminToken(token);
                    return new Result(200,"success","登陆成功", superAdmin);
                }


            }
            else{
                if (superAdminInfoDO1.getAdminTimes()==4){
                    superAdminInfoDO1.setAdminIsFrozen(true);
                    superAdminInfoDO1.setAdminTime(sdf.format(date));
                    superAdminInfoDO1.setAdminTimes(5);
                    superAdminService.update(superAdminInfoDO1);
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else if(superAdminInfoDO1.getAdminTimes()==5){
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else{
                    int i = superAdminInfoDO1.getAdminTimes();
                    i++;
                    superAdminInfoDO1.setAdminTimes(i);
                    int ch = 5-i;
                    superAdminService.update(superAdminInfoDO1);
                    return new Result(400,"error","用户名密码错误,您还有"+ch+"次尝试机会","null");
                }
            }
        }
        else{
            return new Result(400,"error","用户名不存在","null");
        }
    }

    @ResponseBody
    @PostMapping(value = "/adminLogin")
    @ApiOperation("管理员登录接口")
    public Result adminLogin(@RequestParam("username") String username,
                             @RequestParam("password") String password) throws ParseException {
        AdminInfoDO adminInfoDO1 = adminService.getAdminInfoByName(username);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        Date d1 = sdf.parse(now);

        //用户名存在
        if (adminInfoDO1 != null) {
            if(password.equals(adminInfoDO1.getAdminPassword())){
                if(adminInfoDO1.getAdminIsFrozen()==true){
                    String frozen = adminInfoDO1.getAdminTime();
                    Date d2 = sdf.parse(frozen);
                    if((((d1.getTime() - d2.getTime()) / 1000) / 60)>=10){
                        adminInfoDO1.setAdminTimes(0);
                        adminInfoDO1.setAdminIsFrozen(false);
                        adminInfoDO1.setAdminTime(null);
                        adminService.updateAdminInfo(adminInfoDO1);
                        String token = tokenService.getAdminToken(adminInfoDO1);
                        adminInfoDO1.setAdminToken(token);
                        adminInfoDO1.setAdminIsAdmin(true);
                        adminInfoDO1.setAdminIsSuperAdmin(false);
                        adminInfoDO1.setAdminPassword(null);
                        return new Result(200,"success","登陆成功", adminInfoDO1);
                    }
                    else{
                        long s = 10-(((d1.getTime() - d2.getTime()) / 1000) / 60);
                        return new Result(400,"error","账号冻结中，预计还有"+s+"分钟", null);
                    }
                }
                else{
                    adminInfoDO1.setAdminTimes(0);
                    adminService.updateAdminInfo(adminInfoDO1);
                    String token = tokenService.getAdminToken(adminInfoDO1);
                    adminInfoDO1.setAdminIsAdmin(true);
                    adminInfoDO1.setAdminIsSuperAdmin(false);
                    adminInfoDO1.setAdminToken(token);
                    adminInfoDO1.setAdminPassword(null);
                    return new Result(200,"success","登陆成功", adminInfoDO1);
                }
            }
            else{
                if (adminInfoDO1.getAdminTimes()==4){
                    adminInfoDO1.setAdminIsFrozen(true);
                    adminInfoDO1.setAdminTime(sdf.format(date));
                    adminInfoDO1.setAdminTimes(5);
                    adminService.updateAdminInfo(adminInfoDO1);
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else if(adminInfoDO1.getAdminTimes()==5){
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else{
                    int i = adminInfoDO1.getAdminTimes();
                    i++;
                    adminInfoDO1.setAdminTimes(i);
                    int ch = 5-i;
                    adminService.updateAdminInfo(adminInfoDO1);
                    return new Result(400,"error","用户名密码错误,您还有"+ch+"次尝试机会","null");
                }
            }
        }
        else{
            return new Result(400,"error","用户名不存在","null");
        }
    }

    @ResponseBody
    @PostMapping(value = "/userLogin")
    @ApiOperation("用户登录接口")
    public Result userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password) throws ParseException {
        UserInfoDO userInfoDO1 = userService.getUserByName(username);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        Date d1 = sdf.parse(now);

        //用户名存在
        if (userInfoDO1 != null) {
            if(password.equals(userInfoDO1.getUserInfoPassword())){
                if(userInfoDO1.getUserInfoIsFrozen()==true){
                    String frozen = userInfoDO1.getUserInfoTime();
                    Date d2 = sdf.parse(frozen);
                    if((((d1.getTime() - d2.getTime()) / 1000) / 60)>=10){
                        userInfoDO1.setUserInfoTimes(0);
                        userInfoDO1.setUserInfoIsFrozen(false);
                        userInfoDO1.setUserInfoTime(null);
                        userService.updateUser(userInfoDO1);
                        String token = tokenService.getUserToken(userInfoDO1);
                        userInfoDO1.setUserInfoToken(token);
                        userInfoDO1.setUserInfoPassword(null);
                        return new Result(200,"success","登陆成功", userInfoDO1);
                    }
                    else{
                        long s = 10-(((d1.getTime() - d2.getTime()) / 1000) / 60);
                        return new Result(400,"error","账号冻结中，预计还有"+s+"分钟", null);
                    }
                }
                else{
                    userInfoDO1.setUserInfoTimes(0);
                    userService.updateUser(userInfoDO1);
                    String token = tokenService.getUserToken(userInfoDO1);
                    userInfoDO1.setUserInfoToken(token);
                    userInfoDO1.setUserInfoPassword(null);
                    return new Result(200,"success","登陆成功", userInfoDO1);
                }
            }
            else{
                if (userInfoDO1.getUserInfoTimes()==4){
                    userInfoDO1.setUserInfoIsFrozen(true);
                    userInfoDO1.setUserInfoTime(sdf.format(date));
                    userInfoDO1.setUserInfoTimes(5);
                    userService.updateUser(userInfoDO1);
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else if(userInfoDO1.getUserInfoTimes()==5){
                    return new Result(400,"error","用户名密码错误,账户已冻结，请十分钟后再次尝试","null");
                }
                else{
                    int i = userInfoDO1.getUserInfoTimes();
                    i++;
                    userInfoDO1.setUserInfoTimes(i);
                    int ch = 5-i;
                    userService.updateUser(userInfoDO1);
                    return new Result(400,"error","用户名密码错误,您还有"+ch+"次尝试机会","null");
                }
            }
        }
        else{
            return new Result(400,"error","用户名不存在","null");
        }


    }

}
